﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace ET.Monitor.DAL
{
    public class DistrictDAL
    {
        object objJudge = null, obj = null, dlVal = null;
        string strSql = "", strErrMsg = "";
        DataTable dt = new DataTable();

        InvokeWebService iw = new InvokeWebService();
        string rtdbRelationAddress = "";
        string rtdbRealTimeAddress = "";
        string rtdbNameSpace = "";
        string rtdbJudge = "";

        public DistrictDAL()
        {
            rtdbRelationAddress = ConfigurationManager.ConnectionStrings["RelationURL"].ConnectionString;
            rtdbRealTimeAddress = ConfigurationManager.ConnectionStrings["RealTimeURL"].ConnectionString;
            rtdbNameSpace = ConfigurationManager.ConnectionStrings["NameSpace"].ConnectionString;
            rtdbJudge = ConfigurationManager.ConnectionStrings["Judge"].ConnectionString;
        }


        #region 获取某个区域所有逆变器实时发电量
        /// <summary>
        /// 功能描述：获取某个区域所有逆变器实时发电量
        /// 创    建：胡进财  日    期：20141016
        /// <summary>
        /// <param name="id">区域编码</param>
        /// <returns></returns>
        public object[] GetDistrictRealDl(string id)
        {
            object[] val = null;
            strSql = "select T_ELECTTAG from T_BASE_POINTS_UNIT where T_UNITID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID='" + id + "')";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            if (dt != null && dt.Rows.Count > 0)
            {
                val = new object[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i][0] != null)
                        if (rtdbJudge == "1")
                            dlVal = Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() }));
                        else
                            dlVal = Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() }));
                    val[i] = dlVal;
                }
            }
            return val;
        }
        #endregion

        #region 获取某个区域所有逆变器负荷
        /// <summary>
        /// 功能描述：获取某个区域所有逆变器负荷
        /// 创    建：胡进财  日    期：20141016
        /// <summary>
        /// <param name="id">区域编码</param>
        /// <returns></returns>
        public object[] GetDistrictPower(string id)
        {
            object[] val = null;
            strSql = "select T_POWERTAG from T_BASE_POINTS_UNIT where T_UNITID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID='" + id + "')";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            if (dt != null && dt.Rows.Count > 0)
            {
                val = new object[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i][0] != null)
                        if (rtdbJudge == "1")
                            dlVal = Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() }));
                        else
                            dlVal = Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() }));
                    val[i] = dlVal;
                }
            }
            return val;
        }
        #endregion

        #region 获取某个区域所有逆变器信息(编码)
        /// <summary>
        /// 功能描述：获取某个区域所有逆变器信息(编码)
        /// 创    建：胡进财  日    期：20141016
        /// <summary>
        /// <param name="id">区域编码</param>
        /// <returns></returns>
        public DataTable GetInverterOrder(string id)
        {
            strSql = "select I_ORDER from T_BASE_UNIT where T_DISTRICTID='" + id + "' order by I_ORDER asc";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 获取逆变器信息
        public DataTable GetInverter(string id)
        {
            if (id.IndexOf("'") > -1)
                strSql = "select T_DISTRICTID,T_UNITDESC_CN,T_UNITDESC_EN,T_UNITID from T_BASE_UNIT where T_DISTRICTID in(" + id + ") and T_MACHINEID in(select T_MACHINEID from T_BASE_MACHINE where T_TYPEID='nbq')  order by I_ORDER asc";
            else
                strSql = "select T_DISTRICTID,T_UNITDESC_CN,T_UNITDESC_EN,T_UNITID from T_BASE_UNIT where T_DISTRICTID ='" + id + "' and T_MACHINEID in(select T_MACHINEID from T_BASE_MACHINE where T_TYPEID='nbq')  order by I_ORDER asc";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            return dt;
        }
        #endregion

        #region 获取逆变器信息
        public DataTable GetDevice(string id)
        {
            if (id.IndexOf("'") > -1)
                strSql = "select T_DISTRICTID,T_UNITDESC_CN,T_UNITDESC_EN,T_UNITID,e.T_TYPEDESC_CN from T_BASE_UNIT u  left join T_BASE_MACHINE m on u.T_MACHINEID = m.T_MACHINEID left join T_BASE_EQUIPTYPE e on m.T_TYPEID=e.T_TYPEID where T_DISTRICTID in(" + id + ")";
            else
                strSql = "select T_DISTRICTID,T_UNITDESC_CN,T_UNITDESC_EN,T_UNITID,e.T_TYPEDESC_CN from T_BASE_UNIT u  left join T_BASE_MACHINE m on u.T_MACHINEID = m.T_MACHINEID left join T_BASE_EQUIPTYPE e on m.T_TYPEID=e.T_TYPEID where T_DISTRICTID ='" + id + "'";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            return dt;
        }
        #endregion


        #region 根据区域编码获取区域信息
        /// <summary>
        /// 根据区域编码获取区域信息
        /// 创    建：刘重阳  
        /// 日    期：20150106
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetDistrictInfoById(string id)
        {
            strSql = "SELECT T_ORGID ,T_DISTRICTID ,T_DISTRICTDESC_CN ,T_DISTRICTDESC_EN FROM dbo.T_BASE_DISTRICT WHERE T_DISTRICTID='" + id + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            return dt;
        }
        #endregion

        #region 新增区域
        /// <summary>
        /// 新增区域
        /// 创    建：刘重阳 
        /// 日    期：20150106 
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="parentId"></param>
        /// <param name="orgNameCn"></param>
        /// <param name="orgNameEn"></param>
        /// <returns></returns>
        public bool AddDistrictModel(string orgId, string parentId, string orgNameCn)
        {
            strSql = "INSERT INTO T_BASE_DISTRICT VALUES('" + parentId + "','" + orgNameCn + "','" + orgId + "','');";
            return (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { strSql, strErrMsg });
        }
        #endregion

        #region 根据区域编号删除区域信息
        /// <summary>
        /// 根据区域编号删除区域信息
        /// 创    建：刘重阳  
        /// 日    期：20150107
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteDistrictModelById(string id)
        {
            strSql = "DELETE FROM T_BASE_DISTRICT WHERE T_DISTRICTID='" + id + "'";
            return (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { strSql, strErrMsg });
        }
        #endregion

        #region 根据区域编码修改区域信息
        /// <summary>
        /// 根据区域编码修改区域信息
        /// 创    建：刘重阳  
        /// 日    期：20150107
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdateDistrict(string id, string nameCn)
        {
            strSql = "UPDATE T_BASE_DISTRICT SET T_DISTRICTDESC_CN='" + nameCn + "' WHERE T_DISTRICTID='" + id + "'";
            return (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { strSql, strErrMsg });
        }
        #endregion
    }
}
